package com.baizhi.springbootshiro.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author Administrator
 */
@Slf4j
@Component
public class ContextBeanUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("ioc容器:{}", applicationContext);
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name : names) {
            log.info("容器内组件:{}", name);
        }
        ContextBeanUtils.applicationContext = applicationContext;
    }

    /**
     * getBean
     *
     * @param beanName implNames
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String beanName, Class<T> clazz) {
        return applicationContext.getBean(beanName, clazz);
    }
}
